package com.easygem.controller.business;

import com.easygem.business.service.*;
import com.easygem.common.model.business.*;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.response.ResponseMessage;
import com.easygem.common.vo.business.BgClientVO;
import com.easygem.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;
import java.util.Map;

/**
 * @author 肖智
 * @version 1.0
 * @Description TODO
 * @date 2025/1/14
 */
@Api(tags = "客户")
@RestController
@RequestMapping("/bgClient")
public class BgClientController {
    @Autowired
    private BgClientService bgClientService;
    @Autowired
    private BgIndustryService bgIndustryService;
    @Autowired
    private BgClientTypeService bgClientTypeService;
    @Autowired
    private BgSourceService bgSourceService;
    @Autowired
    private BgClientCoilInService bgClientCoilInService;
    @Autowired
    private BgClientStatusService bgClientStatusService;
    @Autowired
    private BgClientFollowService bgClientFollowService;

    @ApiOperation(value = "客户列表", notes = "模糊查询客户列表")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<PageVO<BgClient>> getCustomerList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                           BgClientVO bgClientVO) {
        PageVO<BgClient> resourceInfoList = bgClientService.selectBgClientList(pageNum, pageSize, bgClientVO);
        return ResponseBean.success(resourceInfoList);
    }


    /**
     * 批量删除客户
     */
    @ApiOperation(value = "批量删除客户", notes = "批量删除客户")
    @PostMapping("/deleteBgClients")
    @RequiresPermissions("ClientManage:delete")
    @ResponseBody
    public Integer deleteBgClients(@RequestBody Long[] clientIds) {
        Integer deleteCount = 0;
        for (Long clientId : clientIds) {
            Integer result = bgClientService.deleteBgClientByClientId(clientId);
            if (result > 0) {
                deleteCount++; // 统计成功删除的数量
            }
        }
        return deleteCount;
    }

    /**
     * 添加客户
     */
    @ApiOperation(value = "添加客户", notes = "添加客户")
    @PostMapping("/addBgClients")
    @RequiresPermissions("ClientManage:add")
    @ResponseBody
    public Integer addBgClients(@RequestBody Map<String, Object> map) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long empId = activeUser.getEmployee().getEmpId();
        Timestamp createTime = Timestamp.from(Instant.now());
        map.put("createTime", createTime);
        map.put("employeeIdAftermarket", empId);
        return bgClientService.addBgClient(map);
    }

    /**
     * 修改客户
     */
    @ApiOperation(value = "修改客户", notes = "修改客户")
    @PostMapping("/editBgClient")
    @RequiresPermissions("ClientManage:edit")
    @ResponseBody
    public Integer editBgClient(@RequestBody Map<String, Object> map) {
        return bgClientService.editBgClient(map);
    }

    /**
     * 加关注
     */
    @ApiOperation(value = "加关注", notes = "加关注")
    @RequiresPermissions("ClientManage:addAttention")
    @PostMapping("/addAttention")
    @ResponseBody
    public Integer addAttention(@RequestBody Long clientId) {
        return bgClientService.addAttention(clientId);
    }

    /**
     * 取消关注
     */
    @ApiOperation(value = "取消关注", notes = "取消关注")
    @PostMapping("/cancelAttention")
    @RequiresPermissions("ClientManage:cancelAttention")
    @ResponseBody
    public Integer cancelAttention(@RequestBody Long clientId) {
        return bgClientService.cancelAttention(clientId);
    }


    /**
     * 添加客户跟进
     */
    @ApiOperation(value = "添加客户跟进", notes = "添加客户跟进")
    @PostMapping("/addBgClientFollow")
    @RequiresPermissions("ClientManage:follow")
    @ResponseBody
    public ResponseBean<Integer> addBgClientFollow(@RequestBody BgClientFollow bgClientFollow) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long empId = activeUser.getEmployee().getEmpId();
        bgClientFollow.setEmpId(empId);
        bgClientFollow.setFollowTime(Timestamp.from(Instant.now()));
        Integer result = bgClientFollowService.addBgClientFollow(bgClientFollow);
        return ResponseBean.success(result);
    }

    /**
     * 客户跟进详情
     */
    @ApiOperation(value = "跟进详情", notes = "根据客户编号获取跟进详情")
    @GetMapping("/getBgClientFollowListByClientId")
    @ResponseBody
    public ResponseMessage<List<BgClientFollow>> getBgClientFollowListByClientId(@RequestParam(value = "clientId") Long clientId) {
        List<BgClientFollow> BgClientFollowList = bgClientFollowService.getBgClientFollowListByClientId(clientId);
        return ResponseMessage.success(BgClientFollowList);
    }


    /**
     * 行业类型列表
     */
    @ApiOperation(value = "行业类型列表", notes = "查询所有行业类型")
    @GetMapping("/findBgIndustryList")
    @ResponseBody
    public List<BgIndustry> findBgIndustryList() {
        return bgIndustryService.findBgIndustryList();
    }

    /**
     * 客户种类列表
     */
    @ApiOperation(value = "客户种类列表", notes = "查询所有客户种类")
    @GetMapping("/findBgClientTypeList")
    @ResponseBody
    public List<BgClientType> findBgClientTypeList() {
        return bgClientTypeService.findBgClientTypeList();
    }

    /**
     * 客户来源列表
     */
    @ApiOperation(value = "客户来源列表", notes = "客户来源列表")
    @GetMapping("/findBgSourceList")
    @ResponseBody
    public List<BgSource> findBgSourceList() {
        return bgSourceService.selectBgSource();
    }

    /**
     * 客户进线
     */
    @ApiOperation(value = "客户进线", notes = "客户进线")
    @GetMapping("/findBgClientCoilIn")
    @ResponseBody
    public List<BgClientCoilIn> findBgClientCoilIn() {
        return bgClientCoilInService.selectBgClientCoilIn();
    }

    /**
     * 客户状态
     */
    @ApiOperation(value = "客户状态", notes = "客户状态")
    @GetMapping("/findBgClientStatus")
    @ResponseBody
    public List<BgClientStatus> findBgClientStatus() {
        return bgClientStatusService.findBgClientStatusList();
    }


}
